package org.dromara.mall.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.Now;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.mall.domain.MallGoodsCategory;
import org.dromara.mall.domain.vo.CategoryVo;
import org.dromara.mall.mapper.MallGoodsCategoryMapper;
import org.dromara.mall.service.IMallGoodsCategoryService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 商品分类Service业务层处理
 *
 * @author MrLei
 * @date 2024-05-19
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class MallGoodsCategoryServiceImpl implements IMallGoodsCategoryService {

    private final MallGoodsCategoryMapper baseMapper;

    /**
     * 查询商品分类
     */
    @Override
    public MallGoodsCategory queryById(Long id){
        return baseMapper.selectById(id);
    }


    /**
     * 查询商品分类列表
     */
    @Override
    public List<MallGoodsCategory> queryList(MallGoodsCategory bo) {
        LambdaQueryWrapper<MallGoodsCategory> lqw = buildQueryWrapper(bo);
        long currentTimeMillis1 = System.currentTimeMillis();
        // 查询所有分类
        List<MallGoodsCategory> catelist = baseMapper.selectList(lqw);

        log.info("查询所有分类耗时："+ (System.currentTimeMillis()-currentTimeMillis1)+ "毫秒");

        //TODO 这里不用递归方法
        //1、查询第一层
        long currentTimeMillis2 = System.currentTimeMillis();
        List<MallGoodsCategory> oneList = catelist.stream().filter(x -> x.getParentId()==0).toList();
        log.info("查询第一层耗时："+ (System.currentTimeMillis()-currentTimeMillis2)+ "毫秒");

        //2、查询第二层
        long currentTimeMillis3 = System.currentTimeMillis();
        oneList.forEach(one->{
            List<MallGoodsCategory> twoList = catelist.stream()
                .filter(x -> Objects.equals(x.getParentId(), one.getId())).toList();
            //封装
            one.setChildren(twoList);
            //查询第三层
            twoList.forEach(two->{
                List<MallGoodsCategory> threeList = catelist.stream()
                    .filter(x -> Objects.equals(x.getParentId(), two.getId())).toList();
                //封装
                two.setChildren(threeList);
            });
        });
        log.info("查询二三层耗时："+ (System.currentTimeMillis()-currentTimeMillis3)+ "毫秒");
        return oneList;
    }


    private LambdaQueryWrapper<MallGoodsCategory> buildQueryWrapper(MallGoodsCategory bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallGoodsCategory> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), MallGoodsCategory::getName, bo.getName());
        lqw.eq(ObjectUtil.isNotNull(bo.getStatus()) , MallGoodsCategory::getStatus, bo.getStatus());
        lqw.eq(ObjectUtil.isNotNull(bo.getSort()), MallGoodsCategory::getSort, bo.getSort());
        return lqw;
    }

    /**
     * 新增商品分类
     */
    @Override
    public Boolean insertByBo(MallGoodsCategory bo) {
        validEntityBeforeSave(bo);
        boolean flag = baseMapper.insert(bo) > 0;
        if (flag) {
            bo.setId(bo.getId());
        }
        return flag;
    }

    /**
     * 修改商品分类
     */
    @Override
    public Boolean updateByBo(MallGoodsCategory bo) {
        validEntityBeforeSave(bo);
        return baseMapper.updateById(bo) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MallGoodsCategory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品分类
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
    /*获取父级分类ID*/
    @Override
    public CategoryVo selectParentId(Long id) {
        LambdaQueryWrapper<MallGoodsCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallGoodsCategory::getId,id);
        //3级详情
        MallGoodsCategory category3 = baseMapper.selectById(id);
        //2级详情
        MallGoodsCategory category2 = baseMapper.selectById(category3.getParentId());
        //1级详情
        MallGoodsCategory category1 = baseMapper.selectById(category2.getParentId());
        //返回数据
        CategoryVo categoryVo = new CategoryVo();
        categoryVo.setOneId(category1.getId());
        categoryVo.setTwoId(category2.getId());
        categoryVo.setThreeId(category3.getId());
        return categoryVo;
    }
}
